/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ace.game;

import ace.system.xml.XmlNode;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Hallucinogen
 */
public class GradientLine {
	public GradientLine(int fromColor, int toColor, int inX1, int inY1, int inX2, int inY2) {
		mFromColor	= fromColor;
		mToColor	= toColor;
		x1			= inX1;
		y1			= inY1;
		x2			= inX2;
		y2			= inY2;

        mLengthMultiplier = 1;
	}

	// make gradient line from xml file
	public GradientLine(XmlNode XML) {
        mLengthMultiplier = 1;
		for (int i = 0, n = XML.getChildren().size(); i < n; ++i) {
			XmlNode Node	= (XmlNode)XML.getChildren().elementAt(i);
			String Tag		= Node.getNodeName();

			if (Tag.equals("from"))	 {
				int R = Integer.parseInt(Node.getAttribute("r"));
				int G = Integer.parseInt(Node.getAttribute("g"));
				int B = Integer.parseInt(Node.getAttribute("b"));

				mFromColor = convertColor(R, G, B);
			} else if (Tag.equals("to")) {
				int R = Integer.parseInt(Node.getAttribute("r"));
				int G = Integer.parseInt(Node.getAttribute("g"));
				int B = Integer.parseInt(Node.getAttribute("b"));

				mToColor = convertColor(R, G, B);
			}
            else if (Tag.equals("detail"))      mDetail             = Integer.parseInt(((XmlNode)Node.getChildren().elementAt(0)).getNodeValue());
            else if (Tag.equals("multiplier"))  mLengthMultiplier   = Integer.parseInt(((XmlNode)Node.getChildren().elementAt(0)).getNodeValue());
		}
	}

	public void setFromColor(int r, int g, int b) {
		mFromColor = convertColor(r, g, b);
	}

	public void setToColor(int r, int g, int b) {
		mToColor = convertColor(r, g, b);
	}

	private int convertColor(int r, int g, int b) {
		int res = 0;

		res |= r << 16;
		res |= g << 8;
		res |= b;

		return res;
	}

	private int interpolateColor(int from, int to, int detail, int detailTarget) {
		int fromR = (from >> 16) % 256;
		int fromG = (from >> 8) % 256;
		int fromB = from % 256;

		int toR = (to >> 16) % 256;
		int toG = (to >> 8) % 256;
		int toB = to % 256;

		int resR = fromR + (toR - fromR) * detail / detailTarget;
		int resG = fromG + (toG - fromG) * detail / detailTarget;
		int resB = fromB + (toB - fromB) * detail / detailTarget;

		return convertColor(resR, resG, resB);
	}

	public void draw(Graphics g) {
		int dx = (x2 - x1) * mLengthMultiplier / mDetail;
		int dy = (y2 - y1) * mLengthMultiplier / mDetail;

		// temporary variable
		int color1 = mFromColor;
		int tempX1 = x1;
		int tempY1 = y1;
		for (int i = 1; i <= mDetail; ++i) {
			g.setColor(color1);
			g.drawLine(tempX1, tempY1, tempX1 + dx, tempY1 + dy);
			color1 = interpolateColor(mFromColor, mToColor, i, mDetail);
			tempX1 += dx;
			tempY1 += dy;
		}
	}

	public int getDetail()		{ return mDetail;		}
	public int getFromColor()	{ return mFromColor;	}
	public int getToColor()		{ return mToColor;		}

	private int mFromColor;
	private int mToColor;
	private int mDetail;
    private int mLengthMultiplier;
	public int x1, x2, y1, y2;
}
